package cn.wolfcode.crm.shiro;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class ExceptionHandler {

    @org.springframework.web.bind.annotation.ExceptionHandler(UnauthorizedException.class)
    public void exceptionHandler(HandlerMethod method, HttpServletResponse resp) throws IOException {
        //根据是否有ResponseBody判断是否是  ajax请求
        ResponseBody body = method.getMethodAnnotation(ResponseBody.class);
        if(body != null){   //是ajax请求
            //将返回的参数封装为 json  然后返回
            resp.setContentType("text/json;charset=UTF-8");
            resp.getWriter().write("{\"success\":false,\"msg\":null,\"total\":0,\"rows\":[]}");
        }else{  //普通请求,跳转到权限不足界面
            resp.setContentType("text/html;charset=UTF-8");
            resp.sendRedirect("/nopermission.jsp");
        }

    }
}
